package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.Tasks;
import unipi.database.User;
import unipi.helpers.HTMLPages;
import unipi.helpers.LoginCookie;
import unipi.helpers.Table;

public class ViewTasks extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public ViewTasks()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	}
			
	private String getPage(String uid, String view, String userID)
	{		
		List<List<String>> taskList;
		String title;
		
		if(view.equals("completed"))
		{
			title = "Ολοκληρωμένες Εργασίες";
			taskList = Tasks.getCompleted(userID);
		}
		else if(view.equals("notstarted"))
		{
			title = "Εργασίες που δεν έχουν ξεκινήσει";
			taskList = Tasks.getNotStarted(userID);
		}
		else if(view.equals("comingdue"))
		{
			title = "Επείγουσες Εργασίες";
			taskList = Tasks.getComingDue(userID);
		}
		else if(view.equals("overdue"))
		{
			title = "Αργοπορημένες Εργασίες";
			taskList = Tasks.getOverDue(userID);
		}
		else if(view.equals("mytasks"))
		{
			title = "Οι εργασίες μου";
			taskList = Tasks.getAssigned(userID);
		}
		else if(view.equals("shouldstarted"))
		{
			title = "Εργασίες που έπρεπε να έχουν ξεκινήσει";
			taskList = Tasks.getShouldHaveStarted(userID);
		}
		else
		{
			title = "Τρέχουσες Εργασίες";
			taskList = Tasks.getPending(userID);
		}
		
		String content = "<h1>" + title + "</h1>";
		
		if(uid != userID)
		{
			Map<String, String> map = User.getUserData(userID);
			if(map != null)
				content += "<h2 class=\"subtitle\">του χρήστη " + map.get("surname") + " " + map.get("name") + "</h2>";
		}
		
		if(taskList == null) content += "Πρόβλημα με την φόρτωση των εργασιών σας.";
		else if(taskList.size() == 1) content += "Δεν υπάρχουν εργασίες.";
		else
		{
			if(uid != userID && !view.equals("completed")) // remove add progress if you are viewing someone else's tasks
			{
				for(List<String> row : taskList)
				{
					row.remove(row.size() - 1);					
				}				
			}
			
			List<String> groups = new ArrayList<String>();
			
			for(List<String> row : taskList)
			{
				groups.add(row.remove(1));
			}
			
			Table t = new Table(taskList.get(taskList.size() - 1));
			
			String lastGroup = groups.get(0);
			String currGroup;
			t.addFillerRow(lastGroup, false);				
			
			for(int x = 0; x < taskList.size() - 1; x++)
			{
				currGroup = groups.get(x);
				
				if(!lastGroup.equals(currGroup))
				{
					lastGroup = currGroup;
					t.addFillerRow(currGroup, false);					
				}
				
				t.addRow(taskList.get(x), true);
			}
			
			content += t.getHTML();	
		}			
		
		return HTMLPages.getGeneralPage(content, uid);		
	}	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			String view = request.getParameter("view");
			String userID = request.getParameter("uid");
			if(view == null) view = "pending";			
			if(userID == null) userID = uid;
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			out.println(getPage(uid, view, userID));
			
			out.close();			
		}
		
	}
}